﻿





$.fn.fadeable = function (options) {

    var settings = {
        target: this,
        duration: 'fast'
    };
    $.extend(settings, options)

    $(this).live('hover', function (event) {
        switch (event.type) {
            case 'mouseenter':
                $(this).find(settings.target).stop().fadeTo(settings.duration, 1);
                break;
            case 'mouseleave':
                $(this).find(settings.target).stop().fadeTo(settings.duration, 0);
                break;
        }
    });
}



function updateTips(t) {
    tips.text(t).addClass("ui-state-highlight");
    setTimeout(function () {
        tips.removeClass("ui-state-highlight", 1500);
    }, 500);
}

function checkLength(o, n, min, max) {
    if (o.val().length > max || o.val().length < min) {
        o.addClass("ui-state-error");
        updateTips("Length of " + n + " must be between " +
					min + " and " + max + ".");
        return false;
    } else {
        return true;
    }
}

function checkRegexp(o, regexp, n) {
    if (!(regexp.test(o.val()))) {
        o.addClass("ui-state-error");
        updateTips(n);
        return false;
    } else {
        return true;
    }
}

$(document).ready(function () {



    $('.simple_color').simpleColor();
	
   
	

    $("#logOutBtn").click(function () {


        window.location.replace("/logout");


    });


    $(".deleteBtn").click(function () {



        var bname = $("#boardName").val();

        var msgID = $(this).attr('id');
        var request = $.ajax({
            type: 'POST',
            url: "/deletemsg",
            dataType: "json",
            data: { boardName: bname, id: msgID }


        });
        request.done(function (data) {


            if (data.redirect) {
                // data.redirect contains the string URL to redirect to
                window.location.replace(data.redirect);
            }else  if (data.msgId) {

                $("#msg"+data.msgId ).animate({
                    opacity: 0.55,
                    
                    height: 'toggle'
                }, 500, function () { $(this).remove(); });
            }
            else {
                
            }



        });


    });



            

    $("#dialog-form").dialog({
        autoOpen: false,
        height: 420,
        width: 550,
        modal: true,
        buttons: {
            "Create message": function () {
                var bValid = true;




                var bname = $("#boardName").val();

                var nsubject = $("#subject").val();
                var ntext = $("#text").val();
                var ncolor = $(".simple_color").val();
                var request = $.ajax({
                    type: 'POST',
                    url: "/addmsg",
                    dataType: "json",
                    data: { boardName: bname, subject: nsubject, text: ntext, color: ncolor }


                });
                request.done(function (data) {

                    if (data.redirect) {
                        // data.redirect contains the string URL to redirect to
                        window.location.replace(data.redirect);
                    }
                    else {

                    }



                });





            },
            Cancel: function () {
                $(this).dialog("close");
            }
        },
        close: function () {
          
        }
    });

    $("#createNewBoard").click(function () {




        $("#dialog-form").dialog("open");
    });





    $('ol.dribbbles li div.dribbble-img').fadeable({ target: 'a.dribbble-over' });







});


              